from odv_env import add_path_dll_directories
add_path_dll_directories()

from typing import Optional

from pyodv import core as odvCore

def addColorMap(viewer : odvCore.Viewer)->Optional[str]:
    options = odvCore.ColorMapOptions()
    options.urls = ["/home/zhd/oceanDemo/data/daily_hats_2021_106.nc"]
    options.varNames = ["t"]

    options.dimOrder = odvCore.DataDimensionOrder.DepthLatLong
    options.depthRange = odvCore.DataRange(start=5.0,span=0.0)
    options.latRange  = odvCore.DataRange(start=-90.0,span=180.0)
    options.longRange  = odvCore.DataRange(start=0.0,span=360.0)

    options.colorBarOptions = odvCore.ColorBarOptions(
        colors={
            0.0: odvCore.Color(r=0.0, g=0.0, b=0.0),
            1.0: odvCore.Color(r=1.0, g=1.0, b=1.0)
        },
        minValue=20.0,
        maxValue=30.0
    )

    layerid = viewer.addLayer(options)
    return layerid

def key_callback(viewer:odvCore.Viewer, eventType:odvCore.EventType, key):
    print(f"Key event: {eventType}, key: {key}")

def mouse_callback(viewer:odvCore.Viewer, eventType:odvCore.EventType, button:odvCore.MouseButton, x, y):
    if eventType != odvCore.EventType.MOVE:
        print(f"Mouse event: {eventType}, button: {button}, at ({x}, {y})")

# 模拟带有参数的仿函数
class MouseCallbackWithStatue:
    def __init__(self):
        self.total = 0

    def __call__(self, viewer:odvCore.Viewer, eventType:odvCore.EventType, button:odvCore.MouseButton, x:int, y:int):
        if eventType == odvCore.EventType.PUSH:
            self.total += 1
            print("鼠标点击次数:", self.total)

if __name__ == "__main__":
    view2d = odvCore.Viewer()
    view2d.setUpViewInWindow(100,100,1024,768)
    
    view2d.addKeybordHandler("keyCallback",key_callback)
    view2d.addMouseButtonHandler("mouseCallback",mouse_callback)

    mouseCallbackStatus = MouseCallbackWithStatue()
    view2d.addMouseButtonHandler("mouseCallback2",mouseCallbackStatus)

    a = 0
    while(not view2d.done()):
        view2d.frame()
        a += 1
        if a == 10:
            addColorMap(view2d)
    pass